<?php
/* 
 *  Linonline - OpenSource
 * 
 *  This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Description of Player
 *  This class handles player status, vars and methods.
 * @author Josi
 */
class Player {
    public $authLevel, $hp, $mana, $level, $exp , $gear;

    public function get_authLevel(){
        global $db;
        if(!$_SESSION['userid']){
            // Not loged in -> authLevel = 0
            $this->authLevel = 0;
        }else{
            // Loged in
            $this->authLevel = $db->res("SELECT authlevel FROM accounts WHERE id=".$_SESSION['id_user']);
        }
    }
    public function cookie_login(){
        if(isset($_COOKIE['username']) && isset($_COOKIE['password'])){
            $this->login($_COOKIE['username'], $_COOKIE['password'], 1);
        }
    }
    public function login($username, $password, $remember){
        global $db;
        $res = $db->res("SELECT * FROM accounts WHERE username='$username' AND password='$password'");
        if(!$res){
            return false; 
        }else{
            $_SESSION['userid'] = $res['id'];
            $_SESSION['username'] = $username;
            if($remember == 1){
                setcookie("username", $password, time()+60*60*24*90);
                setcookie("username", $password, time()+60*60*24*90);
            }
            return true;
        }
    }
    public function createAccount(){

    }
    public function createCharacter(){

    }
    public function setMainCharacter(){
        
    }
    public function loadStats(){
        // Testing - Just loading 100/100/1
        $this->hp = 100;
        $this->mp = 100;
        $this->level = 1;
        $this->exp = 0;
    }
    public function Player(){
        // This code runs when an instance of Player is called.
        $this->get_authLevel();
        if($this->authLevel == 0)
                $this->cookie_login();
        else
            $this->loadStats();
    }
}
?>
